<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="备案号/网签号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList()">本地查询</el-button>
        <el-button type="primary" @click="export2Excel('table')">导出</el-button>
        <el-button v-if="isAuth('bdc:bdccontract:save')" type="primary" @click="addOrUpdateHandle()">备案信息查询</el-button>
        <el-button v-if="isAuth('bdc:bdccontract:save')" type="primary" @click="addOrUpdateHandle('1')">网签信息查询</el-button>     
      </el-form-item>
    </el-form>

<el-table
      :data="dataList"
      border

      id="table"
      :height="tableHeight"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;display:none;">

      <el-table-column
        prop="region"
        header-align="center"
        align="center"
        label="区域">
      </el-table-column>
      <el-table-column
        prop="obankid"
        header-align="center"
        width="120px"
        align="center"
        label="发起网点">
      </el-table-column>
      <el-table-column
        prop="ctype"
        header-align="center"
        align="center"
        label="合同类型">
      </el-table-column>
      <el-table-column
        prop="contractno"
        header-align="center"
        align="center"
        label="合同备案号">
      </el-table-column>
      <el-table-column
        prop="signno"
        header-align="center"
        align="center"
        label="网签号">
      </el-table-column>
      <el-table-column
        prop="dealdate"
        header-align="center"
        align="center"
        label="受理时间">
      </el-table-column>
      <el-table-column
        prop="contractid"
        header-align="center"
        align="center"
        label="备案流水号">
      </el-table-column>
      <el-table-column
        prop="recorddate"
        header-align="center"
        width="160px"
        align="center"
        label="备案时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="合同状态">
      </el-table-column>
      <el-table-column
        prop="shard"
        header-align="center"
        align="center"
        label="共有方式">
      </el-table-column>


    </el-table>

    <el-table
      :data="dataList"
      border
      ref="table"
      :height="tableHeight"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">

      <el-table-column
        fixed="left"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="check(scope.row.serialno)" v-if="!scope.row.signno && isAuth('bdc:bdcfirstregist:save')">办理预告业务</el-button>
          <el-button type="text" size="small" @click="checkqwb(scope.row.serialno)" v-if="scope.row.signno && isAuth('bdc:bdcfirstregist:save')">办理全网办业务</el-button>
          <el-button type="text" size="small" @click="personListHandle(scope.row.seqno, scope.row.id)">人员信息</el-button>
          <el-button type="text" size="small" @click="houseListHandle(scope.row.seqno, scope.row.id)">房屋信息</el-button>
          <el-button type="text" size="small" @click="detailHandle(scope.row.serialno)">详情</el-button>
        </template>
      </el-table-column>

      <el-table-column
        prop="region"
        header-align="center"
        align="center"
        label="区域">
      </el-table-column>
      <el-table-column
        prop="obankid"
        header-align="center"
        width="120px"
        align="center"
        label="发起网点">
      </el-table-column>
      <el-table-column
        prop="ctype"
        header-align="center"
        align="center"
        label="合同类型">
      </el-table-column>
      <el-table-column
        prop="contractno"
        header-align="center"
        align="center"
        label="合同备案号">
      </el-table-column>
      <el-table-column
        prop="signno"
        header-align="center"
        align="center"
        label="网签号">
      </el-table-column>
      <el-table-column
        prop="dealdate"
        header-align="center"
        align="center"
        label="受理时间">
      </el-table-column>
      <el-table-column
        prop="contractid"
        header-align="center"
        align="center"
        label="备案流水号">
      </el-table-column>
      <el-table-column
        prop="recorddate"
        header-align="center"
        width="160px"
        align="center"
        label="备案时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="合同状态">
      </el-table-column>
      <el-table-column
        prop="shard"
        header-align="center"
        align="center"
        label="共有方式">
      </el-table-column>


    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="datepicker.pageOptions"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>



    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <notice-register v-if="noticeRegisterVisible" ref="noticeRegister" @refreshDataList="getDataList"></notice-register>
    <personlist v-if="personlistVisible" ref="personlist"  ></personlist>
    <houselist v-if="houselistVisible" ref="houselist"  ></houselist>
    <htba-detail v-if="htbaDetailVisible" ref="htbaDetail"></htba-detail>
    <first-register v-if="firstRegisterVisible" ref="firstRegister"   @refreshDataList="getDataList" ></first-register>    
  </div>
</template>

<script>
  import AddOrUpdate from './bdccontract-add-or-update'
  import NoticeRegister from './notice-register'
  import Personlist from './personlist'
  import Houselist from './houselist'
  import HtbaDetail from './htba-detail'
  import FirstRegister from './bdc-first-register-update' 

  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        tableHeight: 300,
        topHeight: 188,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        noticeRegisterVisible: false,
        personlistVisible: false,
        houselistVisible: false,
        htbaDetailVisible: false,
        firstRegisterVisible: false
      }
    },
    components: {
      AddOrUpdate,
      NoticeRegister,
      Personlist,
      Houselist,
      HtbaDetail,
      FirstRegister
    },
    activated () {
      this.getDataList()
    },
    mounted () {
      this.resetTableHeight()
    },
    methods: {

      export2Excel(id){
          this.datepicker.export2Excel(id, this, '合同备案信息.xlsx')
      },

      resetTableHeight(){
        
        this.$nextTick(function () {
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - this.topHeight;
            
            // 监听窗口大小变化
            let self = this;
            window.onresize = function() {
                self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - self.topHeight
            }
        })
      },

      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/bdc/bdccontract/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },

      check(id){
        this.$http({
          url: this.$http.adornUrl('/bdc/bdcnoticeregist/check'),
          method: 'post',
          data: this.$http.adornData({
            'serialno': id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.noticeRegisterHandle(id);
          } else {
            this.$message.error(data.msg)
          }
     
        })
      },

      checkqwb(id){
        this.$http({
          url: this.$http.adornUrl('/bdc/bdcfirstregist/check'),
          method: 'post',
          data: this.$http.adornData({
            'serialno': id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.qwbHandle(id);
          } else {
            this.$message.error(data.msg)
          }
     
        })
      },

      qwbHandle(id){
          this.firstRegisterVisible = true
          this.$nextTick(() => {
            this.$refs.firstRegister.init(id, 0, '', 1)
          })
      },

      noticeRegisterHandle (id) {
        this.noticeRegisterVisible = true
        this.$nextTick(() => {
          this.$refs.noticeRegister.init(id, 0)
        })
      },
      personListHandle (seqno, id) {
        this.personlistVisible = true
        this.$nextTick(() => {
          this.$refs.personlist.init(seqno, id)
        })
      },
      houseListHandle (seqno, id) {
        this.houselistVisible = true
        this.$nextTick(() => {
          this.$refs.houselist.init(seqno, id)
        })
      },
      detailHandle(serialno){
        this.htbaDetailVisible = true
        this.$nextTick(() => {
          this.$refs.htbaDetail.init(serialno)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.seqno
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/bdc/bdccontract/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style lang="scss">
body .el-table th.gutter{
    display: table-cell!important;

}
</style>